package com.app.service.impl;

import com.app.mapper.*;
import com.app.model.*;
import com.app.service.CategoryService;
import com.app.util.StringTool;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Primary
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    DepartmentMapper departmentMapper;
    @Autowired
    PersonMapper personMapper;
    @Autowired
    ElectronicMapper electronicMapper;
    @Autowired
    ComputerMapper computerMapper;
    @Autowired
    TestsMapper testsMapper;
    @Autowired
    IncidentMapper incidentMapper;
    @Autowired
    TypeMapper typeMapper;


    @Override
    public Page<CategoryModel> categoryPageList(int page, int limit, String name) {
        QueryWrapper<CategoryModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "pid", "name", "sort", "status").like("name", name).orderByAsc("id").orderByDesc("sort");
        Page<CategoryModel> pages = new Page<>();
        pages.setCurrent(page);
        pages.setSize(limit);
        Page<CategoryModel> categoryList = categoryMapper.selectPage(pages, queryWrapper);
        return categoryList;
    }

    @Override
    public List<CategoryModel> selectPerTree(String keyWord) {//全查配置
        QueryWrapper<CategoryModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select();
        if (StringTool.isNotEmpty(keyWord)) {
            queryWrapper.like("name", keyWord);
        }
        List<CategoryModel> modelList = categoryMapper.selectList(queryWrapper);
        return modelList;
    }

    @Override
    public List<CategoryModel> getListByPid() {
        QueryWrapper<CategoryModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "name").ne("pid", 0).eq("status", 1);
        List<CategoryModel> modelList = categoryMapper.selectList(queryWrapper);
        return modelList;
    }

    @Override
    public CategoryModel getPersonCate(String name) {
        QueryWrapper<CategoryModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "name").like("name", name);
        List<CategoryModel> categoryModelList = categoryMapper.selectList(queryWrapper);
        if (categoryModelList.isEmpty()) {
            return null;
        } else {
            return categoryModelList.get(0);
        }
    }

    @Override
    public List<CategoryModel> getCategoryByPid(Integer pid) {
        QueryWrapper<CategoryModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "name").eq("pid", pid);
        List<CategoryModel> modelList = categoryMapper.selectList(queryWrapper);
        return modelList;
    }

    @Override
    public CategoryModel HomeCount() {
        List<CategoryModel> homeCountList = new ArrayList<>();
        CategoryModel categoryModel = new CategoryModel();
        QueryWrapper<DepartmentModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id");
        Integer depCount = departmentMapper.selectCount(queryWrapper);//部门数量
        categoryModel.setDepCount(depCount);
        QueryWrapper<PersonModel> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.select("id");
        Integer personCount = personMapper.selectCount(queryWrapper1);//人员数量
        categoryModel.setPersonCount(personCount);
        QueryWrapper<ElectronicModel> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.select("id");
        Integer electronicCount = electronicMapper.selectCount(queryWrapper2);//电子设备信息数量
        categoryModel.setElectronicCount(electronicCount);
        QueryWrapper<ComputerModel> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.select("id");
        Integer computerCount = computerMapper.selectCount(queryWrapper3);//办公计算机数量
        categoryModel.setComputerCount(computerCount);
        QueryWrapper<TestsModel> queryWrapper4 = new QueryWrapper<>();
        queryWrapper4.select("id");
        Integer testsCount = testsMapper.selectCount(queryWrapper4);//实验数量
        categoryModel.setTestsCount(testsCount);
        QueryWrapper<IncidentModel> queryWrapper5 = new QueryWrapper<>();
        queryWrapper5.select("id");
        Integer incidentCount = incidentMapper.selectCount(queryWrapper5);//事件数量
        categoryModel.setIncidentCount(incidentCount);
        QueryWrapper<TypeModel> queryWrapper6 = new QueryWrapper<>();
        queryWrapper6.select("id");
        Integer typeCount = typeMapper.selectCount(queryWrapper6);//型号信息数量
        categoryModel.setTypeCount(typeCount);
        return categoryModel;
    }

    @Override
    public void insert(CategoryModel model) {
        categoryMapper.insert(model);
    }


    @Override
    public void update(CategoryModel model) {
        categoryMapper.updateById(model);
    }

    @Override
    public void delete(int id) {
        categoryMapper.deleteById(id);
    }

    @Override
    public CategoryModel getCategoryById(int id) {


        return categoryMapper.selectById(id);
    }

    @Override
    public CategoryModel getCatByPidAndId(int pid, int id) {
        QueryWrapper<CategoryModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "pid", "name").eq("pid", pid).eq("id", id);
        CategoryModel model = categoryMapper.selectOne(queryWrapper);
        if (model == null) {
            return null;
        } else {
            return model;
        }
    }
}
